/**
 * Created by cchen on 2015/4/16.
 */
var sharesService = require('../../services/mobile/sharesService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');
var cacheUtil = require('../../common/utils/cacheUtil');

var typeName = {
    qst: "question",
    act: "activity",
    top: "specialTopic",
    app: "app"

}

errorHandler = function (msg, err, req, res) {
    logger.error('Share controller ' + msg + '  - - ' + err.message);
    res.json(messageUtil.composeFailedMessage(req, '无法分享'));
}

queryShareURL = function (req, res, userId, feedsId, targetApp, feedsType) {
    var isExistCallback = function (err, shareObj) {
        if (err) {
            errorHandler("GET  URL Callback", err, req, res);
            return
        }
        if (!shareObj) {
            shareObj = sharesService.addNewShare(userId, feedsId, targetApp, feedsType);
        }
        var url = sharesService.urlGenerate(shareObj);
        res.json(messageUtil.composeSucceededMessage(req, null, {"url": url}));

    };
    sharesService.shareIsExisted(userId, feedsId, targetApp, feedsType, isExistCallback);
}

onError = function (msg, err, req, res) {
    logger.error('Service As User controller ' + msg + '  - - ' + err.message ? err.message : 'error');
    res.json(messageUtil.composeFailedMessage(req, err.message));
}

onSuccess = function (data, req, res) {
    res.json(messageUtil.composeSucceededMessage(req, null, data));
}

onResponse = function (err, data, req, res) {
    if (err) {
        onError(err.message, err, req, res);
    } else {
        onSuccess(data, req, res);
    }
}
exports.shareApp = function (req, res) {
    sharesService.shareApp(req.params.userId, req.params.targetApp, function (err, obj) {
        onResponse(err, obj, req, res);
    });
};

exports.shareSpectialTopic = function (req, res) {
    sharesService.shareSpectialTopic(req.params.userId, req.params.targetApp, req.params.topicId, function (err, obj) {
        onResponse(err, obj, req, res);
    });
};

exports.shareActivity = function (req, res) {
    sharesService.shareActivity(req.params.userId, req.params.targetApp, req.params.activityId, function (err, obj) {
        onResponse(err, obj, req, res);
    });
}

exports.shareQuestion = function (req, res) {
    sharesService.shareQuestion(req.params.userId, req.params.targetApp, req.params.questionId, function (err, obj) {
        onResponse(err, obj, req, res);
    });
}


exports.getSharedFeeds = function (req, res) {
    sharesService.getSharedFeeds(req.params.shareId, req.params.userId, function (err, obj) {
        onResponse(err, obj, req, res);
    });
}

exports.getAppUserShare = function (req, res) {
    var userId = req.params.userId;

    sharesService.getAppUserShareService(userId, function (err, result) {
        onResponse(err, result, req, res);
    })
};

exports.getPartakesByActivityId = function (req, res) {
    sharesService.getPartakesByActivityId(req.params.pageNo, req.params.activityId, function (err, result) {
        onResponse(err, result, req, res);
    })
};

exports.getActivityMessageCode = function (req, res) {
    var phone = req.params.phone;

    var key = 'activity:messageCode:' + phone;

    var message = {
        "mobilePhone": phone,
        "verificationCode": toolUtil.generateVerificationCode(phone, key)
    };
    logger.info(message);
    toolUtil.putMessageVerificationCodeMQ(message);
    res.json(messageUtil.composeSucceededMessage(req, null, null))
};

exports.joinInActivityByWechat = function (req, res) {

    var input = req.body;
    logger.info(input)
    var key = 'activity:messageCode:' + input.phone;

    cacheUtil.cacheGet(key, function (err, code) {
        if (code == null || code != input.messageCode) {
            res.json(messageUtil.composeFailedMessage(req, '验证码错误', null));
        } else {
            var data = {
                number: input.number,
                note: input.note,
                name: input.name,
                phone: input.phone,
                address: input.address,
                partakeType: 2
            };

            sharesService.joinInActivityByWechatService(input.activityId, data, function (err, result) {
                if (err) {
                    res.json(messageUtil.composeFailedMessage(req, err.message, null));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null))
                }
            })
        }
    })
};

